const { Sequelize, DataTypes, Model } = require('sequelize');
///创建数据库连接
const sequelize = new Sequelize('mysql://root:root@localhost:3306/juzao', {
 // 选择一种日志记录参数
 logging: console.log // 默认值,显示日志函数调用的第一个参数
});


var fs = require('fs')
var files = fs.readdirSync('./models')
var models = {}
for(var i = 0; i < files.length; i ++) {
    if(/\.js$/.test(files[i])) {
        let entity = require('./models/' + files[i]),name = files[i].split('.')[0]
        models[name] = sequelize.define(name, entity, { freezeTableName: true })
    }
} 

module.exports = {
    ///初始化数据库
    init(option){
        option = option || {alter: true}
        return sequelize.sync(option)
    },
    ...models
}